apiready = function() {
seajs.use(['../../V', "../../api", "../../jquery", "../../app", "../../service", "../../swiper", "../../dialog"], function(V, $api, $, app, service, Swiper, dialog) {
    var vm = {
        el: '.container',
        data: {
            tabIndex: 0,
            shopInfo: {},
            goodsId: '', // 商品ID
            goodsInfo: {
                tag: 0
            }, // 商品信息
            goodsImages: [], // banner图片列表
            couponList: [], // 优惠券列表
            disCountStr: null, // 展示优惠券文字
            commentList: [], // 评论列表
            commentPage: 1, // 评论列表页数
            hasMoreComment: true, // 是否有更多评论
            isCollect: false, // 是否已经收藏
            collectId: '', // 收藏id
            number: 1, // 购买数量
            choicInfo: null, // 购买的商品
            isSeller: false, // 是否显示购买按钮
            shareShow: false, // 是否显示分享
            tipsInfo: {}, // 商城提示消息
            tipsList: [], // 商城提示列表
            tipsMsg: {}, // 提示内容
            otherUserInfo: {}, // 客服信息
            goodsOptions: [], // 规格信息列表
            chocOption: {}, // 选中的规格信息
            allStock: 0, // 商品总库存
            allCount: 0, // 总销售量
            iconImg: ['../../../image/shop/active-1.png', '../../../image/shop/active-2.png', '../../../image/shop/active-3.png', '../../../image/shop/active-4.png'],
            selectIcon: '../../../image/shop/active-1.png',
            bgColor: ['rgb(255,245,235)', 'rgb(249,246,253)', 'rgb(255,241,244)', 'rgb(245,250,254)'],
            selectColor: '#fff',
            fontColor: ['rgb(251,123,3)', 'rgb(59,151,241)', 'rgb(234,44,88)', 'rgb(127,16,173)'],
            selectFontColor: '#000',
            startTime: '', // 开始时间
            endTime: '', // 秒杀结束时间
            showTime: {
                h: '00',
                m: '00',
                s: '00'
            }, // 倒计时
            isOver: false, // 秒杀时间是否结束
            isNotStart: false, // 秒杀是否已经开始
            userInfo: '', // 获取用户身份
            bs: "", //ajax弹出服务器提示控制变量
            isSelectO: false, // 是否选择了型号
            isClickComment: false,
            cartCount: '', // 购物车数量
        },
        created: function() {
            var that = this;
            if ($api.getStorage("user")) {
                this.userInfo = $api.getStorage("user").data;
                this.bs = 1;
            }
            var goodsId = api.pageParam.goodsId;
            var isSeller = api.pageParam.isSeller;
            this.goodsId = goodsId;

            if (isSeller) {
                this.isSeller = true;
            }

            this.init();

            api.addEventListener({
                name: 'scrolltobottom',
                extra: {
                    threshold: 0 //设置距离底部多少距离时触发，默认值为0，数字类型
                }
            }, function(ret, err) {
                that.scrolltobottom()
            });

            // 前往购物车
            api.addEventListener({
                name: 'toCart'
            }, function(ret, err) {
                if (ret) {
                    app.openWin('buyCart', {
                        channel: "notIndex"
                    });
                }
            });

            // 监听购物车数量改变
            api.addEventListener({
                name: 'lisentCart'
            }, function(ret, err) {
                if (ret) {
                    that.getCartCount();
                }
            });


            // 前往商城首页或者我的名片
            api.addEventListener({
                name: 'shopDetailDoShomeThing'
            }, function(ret, err) {
                if (ret) {
                    var type = ret.value.type;
                    if (type == 0) {
                        // 商城首页
                        if($api.getStorage('user')){
                          api.openWin({
                              name: 'index',
                              // customRefreshHeader: "UIPullRefreshMotive",
                              url: 'widget://src/Shop/shop_index/shop_index.html',

                          })
                        }else{
                          api.openWin({
                              name: 'index',
                              // customRefreshHeader: "UIPullRefreshMotive",
                              url: 'widget://src/Shop/shop_index/shop_index.html',
                              pageParam: {
                                  channel: 'YK'
                              }
                          })
                        }
                    }
                    // else if (type == 1) {
                    //     // 我的名片
                    //     api.closeToWin({
                    //         name: 'root',
                    //         animation: {
                    //             type: 'fade',
                    //             subType: 'from_right',
                    //             duration: 300
                    //         }
                    //     });
                    // }
                }
            });
        },
        methods: {
            init: function() {
                var that = this;
                // 获取商品信息
                this.getGoodsDetail(function() {
                    if (that.goodsInfo.tag == 1) {
                        that.showTime = {
                            h: '00',
                            m: '00',
                            s: '00'
                        }
                        that.countTime();
                        that.$nextTick(function() {
                            $('.js-show').fadeIn('slow');
                        })
                        var startTime = that.startTime.split('-').join('/');
                        if ((new Date(startTime)) > (new Date())) {
                            // 秒杀未开始
                            that.isNotStart = true;
                            $('.js-show').fadeIn('slow');
                        } else {
                            // 开始秒杀
                            var endTime = that.endTime.split('-').join('/');
                            if ((new Date(endTime)) <= (new Date())) {
                                that.isOver = true;
                                that.selectIcon = '../../../image/shop/buy/buy-13.png';
                                that.selectColor = '#fff';
                                that.selectFontColor = '#000';
                                $('.js-show').fadeIn('slow');
                            }
                        }
                    } else {
                        that.$nextTick(function() {
                            $api.setTimeout(function() {
                                $('.js-show').fadeIn('slow');
                                $('.js-show-hd').show();
                            }, 100)
                        });
                    }

                    // 添加足迹
                    that.addUserStep();

                    // 设置优惠券展示文字
                    that.setDiscountText();



                    // 获取购物车数量
                    that.getCartCount();

                    $('.number-goods').on('input', function() {
                        var inputVal = this.value;
                        if (inputVal >= that.chocOption.stock) {
                            this.value = that.chocOption.stock;
                        }
                    });

                    $api.setTimeout(function() {
                        var mySwiper = new Swiper('.swiper-container', {
                            pagination: {
                                el: '.swiper-pagination',
                                type: 'fraction'
                            },
                        })
                    }, 500);
                });
            },

            // 显示分享
            showShare: function() {
                this.shareShow = true;
            },
            hideShare: function() {
                this.shareShow = false;
            },

            // 获取购物车数量
            getCartCount: function() {
                var that = this;
                app.ajax(
                    service['cartGetCartCount'], {
                        data: {
                            values: {
                                unionid: $api.getStorage("shop_unionid"),
                                bs: this.bs
                            }
                        }
                    },
                    function(res) {
                        if (res.code == 0) {
                            that.cartCount = res.data;
                            if (that.cartCount > 0) {
                                that.cartCount > 10 ? $('.js-cart-count').text('10+') : $('.js-cart-count').text(that.cartCount);
                                $('.js-cart-count').show();
                            } else {
                                $('.js-cart-count').hide();
                            }

                        } else {
                            if (that.bs != "") {
                                api.toast({
                                    msg: res.message,
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            }
                        }
                    }
                );
            },




            // 分享到微信
            shareToWx: function(type) {
                var that = this;
                if (this.bs == "") { //游客模式，没有缓存
                    dialog.alert({
                        title: '游客模式',
                        msg: '体验更多功能，请先登录',
                        buttons: ['取消', '确定']
                    }, function(ret) {
                        if (ret.buttonIndex == 2) { //确认
                            api.closeToWin({
                                name: 'WeChat_login'
                            });
                        }
                    })
                }else{
                  var wx = api.require('wx');
                  var toWhere;
                  if (type == 1) {
                      // 分享给朋友
                      toWhere = 'session';
                  } else if (type == 2) {
                      // 分享到朋友圈
                      toWhere = 'timeline';
                  }

                  app.downloadImg(that.goodsInfo.cover, function(img_url) {
                      that.getShareLink(function(url) {

                          wx.shareWebpage({
                              scene: toWhere,
                              title: that.goodsInfo.name,
                              description: that.chocOption.name,
                              contentUrl: url,
                              thumb: img_url
                          }, function(ret, err) {
                              if (ret.status) {
                                  api.toast({
                                      msg: '分享成功',
                                      duration: 2000,
                                      location: 'bottom'
                                  });
                              } else {
                                  api.toast({
                                      msg: '分享失败',
                                      duration: 2000,
                                      location: 'bottom'
                                  });
                              }
                          });
                      });
                  });
                }
            },

            // 获取分享连接
            getShareLink: function(callback) {
                var that = this;
                app.ajax(
                    service['Share'], {
                        data: {
                            values: {
                                type: 2,
                                unionid: $api.getStorage("shop_unionid"),
                                share_id: that.goodsId,
                                from: 'shop_app'
                            }
                        }
                    },
                    function(res) {
                        if (res.code == 0) {
                            var url = res.data;
                            callback(url);
                        } else {
                            api.toast({
                                msg: '获取分享链接失败',
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    }
                );
            },

            // 获取商品详情
            getGoodsDetail: function(cb) {
                var that = this;
                app.ajax(
                    service['getGoodsDetail'], {
                        data: {
                            values: {
                                goods_id: that.goodsId,
                                customer_unionid: $api.getStorage("shop_unionid"),
                                bs: ""
                            }
                        }
                    },
                    function(res) {
                        if (res.code == 0) {
                            that.goodsInfo = res.data;
                            that.shopInfo = res.data.shop;
                            that.goodsImages = res.data.goods_images; // banner图片列表
                            that.couponList = res.data.coupon_list;
                            that.otherUserInfo = res.data.exchange; // 店主信息
                            that.goodsOptions = res.data.goods_options;
                            that.chocOption = res.data.goods_options[0];
                            that.allStock = res.data.shop.all_stock; // 商品总库存
                            that.allCount = res.data.shop.all_count; // 已出售总数
                            that.endTime = res.data.date_end; // 秒杀结束时间
                            that.startTime = res.data.date_start; // 秒杀开始时间

                            // 检测是否收藏
                            if (!res.data.collect_id) {
                                that.isCollect = false;
                            } else {
                                that.isCollect = true;
                                that.collectId = res.data.collect_id;
                            }

                            if (res.data.tag == 1 && !that.isOver) {
                                // 秒杀
                                that.selectIcon = that.iconImg[0];
                                that.selectColor = that.bgColor[0];
                                that.selectFontColor = that.fontColor[0];
                            } else if (res.data.tag == 2) {
                                // 清仓
                                that.selectIcon = that.iconImg[3];
                                that.selectColor = that.bgColor[3];
                                that.selectFontColor = that.fontColor[3];
                            } else if (res.data.tag == 3) {
                                // 折扣
                                that.selectIcon = that.iconImg[2];
                                that.selectColor = that.bgColor[2];
                                that.selectFontColor = that.fontColor[2];
                            } else if (res.data.tag == 4) {
                                // 9.9
                                that.selectIcon = that.iconImg[1];
                                that.selectColor = that.bgColor[1];
                                that.selectFontColor = that.fontColor[1];
                            } else {
                                that.selectIcon = '../../../image/shop/buy/buy-13.png';
                                that.selectColor = '#fff';
                                that.selectFontColor = '#000';
                            }
                            cb();
                        } else {
                            api.toast({
                                msg: res.message,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    }
                );
            },

            // 秒杀倒计时
            countTime: function() {
                var that = this;
                var timer = setInterval(function() {
                    var startTime = that.startTime.split('-').join('/');
                    if ((new Date(startTime)) > (new Date())) {
                        // 秒杀未开始
                        that.isNotStart = true;
                    } else {
                        // 开始秒杀
                        var endTime = that.endTime.split('-').join('/');
                        var now = (new Date(endTime)) - (new Date());
                        var hh = Math.floor(now / (60 * 60 * 1000));
                        var mm = Math.floor(now % (60 * 60 * 1000) / (60 * 1000));
                        var ss = Math.floor(now % (60 * 60 * 1000) % (60 * 1000) / 1000);

                        if ((new Date(endTime)) <= (new Date())) {
                            that.showTime = {
                                h: '00',
                                m: '00',
                                s: '00'
                            }
                            that.isOver = true;
                            that.selectIcon = '../../../image/shop/buy/buy-13.png';
                            that.selectColor = '#fff';
                            that.selectFontColor = '#000';
                        } else {
                            that.showTime = {
                                h: (hh >= 10 ? hh.toString() : "0" + hh),
                                m: (mm >= 10 ? mm.toString() : "0" + mm),
                                s: (ss >= 10 ? ss.toString() : "0" + ss)
                            }
                        }
                    }
                }, 1000);
            },

            // 查看大图
            showBigImg: function(index) {
                var that = this;
                var photoBrowser = api.require('photoBrowser');
                var urlArr = [];
                for (var i = 0; i < that.goodsImages.length; i++) {
                    urlArr.push(that.goodsImages[i].url);
                }

                photoBrowser.open({
                    images: urlArr,
                    bgColor: '#000',
                    activeIndex: index
                }, function(ret, err) {
                    if (ret.eventType == 'click') {
                        photoBrowser.close();
                    }
                });
            },

            // 添加我的足迹
            addUserStep: function() {
                var that = this;

                app.ajax(
                    service['addUserStep'], {
                        data: {
                            values: {
                                goods_id: that.goodsId,
                                unionid: $api.getStorage("shop_unionid"),
                                bs: this.bs
                            }
                        }
                    },
                    function(res) {
                        if (res.code != 0) {
                            if (that.bs != "") {
                                api.toast({
                                    msg: '添加足迹失败',
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            }
                        }
                    }
                );
            },

            // 获取评论列表
            getComment: function() {
                var that = this;
                var page = this.commentPage;
                app.ajax(
                    service['getGoodsComment'], {
                        data: {
                            values: {
                                goods_id: that.goodsId,
                                shop_code: that.shopInfo.shop_code,
                                page: page
                            }
                        }
                    },
                    function(res) {
                        if (res.code == 0) {
                            if (that.commentPage == 1) {
                                that.commentList = res.data;
                            } else {
                                if (res.data.length > 0) {
                                    for (var i = 0, len = res.data.length; i < len; i++) {
                                        that.commentList.push(res.data[i]);
                                    }
                                    that.hasMoreComment = true;
                                } else {
                                    that.hasMoreComment = false;
                                }
                            }
                        } else {
                            api.toast({
                                msg: res.message,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    }
                );
            },

            // 设置优惠券展示文字
            setDiscountText: function() {
                var disCount = this.goodsInfo.coupon_list;
                var str = '';
                var loop = 3;
                if (disCount.length < 3) {
                    if (disCount.length == 0) {
                        return;
                    } else {
                        loop = disCount.length;
                    }
                }
                for (var i = 0; i < loop; i++) {
                    str += '满' + disCount[i].min_goods_amount + '减' + disCount[i].amount + '；';
                }
                this.disCountStr = str;
            },

            // 领取优惠券
            getDiscount: function(id, is_get, idx) {
                var that = this;
                if (this.bs == "") { //游客模式，没有缓存
                    dialog.alert({
                        title: '游客模式',
                        msg: '体验更多功能，请先登录',
                        buttons: ['取消', '确定']
                    }, function(ret) {
                        if (ret.buttonIndex == 2) { //确认
                            api.closeToWin({
                                name: 'WeChat_login'
                            });
                        }
                    })
                }else{
                  if (this.isSeller) {
                      api.toast({
                          msg: '您是商家，不可领取该优惠券',
                          duration: 2000,
                          location: 'bottom'
                      });
                      return;
                  }
                  if (is_get == 1) {
                      api.toast({
                          msg: '您已领取该优惠券',
                          duration: 2000,
                          location: 'middle'
                      });
                      return;
                  }
                  app.ajax(
                      service['getCouponDiscount'], {
                          data: {
                              values: {
                                  unionid: $api.getStorage("shop_unionid"), // 当前用户unionid
                                  coupon_discount_id: id,
                                  shop_code: that.shopInfo.shop_code
                              }
                          }
                      },
                      function(res) {
                          if (res.code == 0) {
                              alert('领取成功');
                              $('.getDiscount').eq(idx).css('background', 'gray');
                          } else {
                              api.toast({
                                  msg: res.message,
                                  duration: 2000,
                                  location: 'bottom'
                              });
                          }
                      }
                  );
                }

            },

            // 收藏、取消收藏
            favorGoods: function() {
                var that = this;
                var isCollect = that.isCollect;

                var url, arg;
                if (this.bs == "") { //游客模式，没有缓存
                    dialog.alert({
                        title: '游客模式',
                        msg: '体验更多功能，请先登录',
                        buttons: ['取消', '确定']
                    }, function(ret) {
                        if (ret.buttonIndex == 2) { //确认
                            api.closeToWin({
                                name: 'WeChat_login'
                            });
                        }
                    })
                } else {
                    if (isCollect) {
                        // 取消收藏
                        url = service['cancelCollectShop'];
                        arg = {
                            unionid: $api.getStorage("shop_unionid"),
                            favorite_id: that.collectId
                        }
                    } else {
                        // 添加收藏
                        url = service['addToFavoriteShop'];
                        arg = {
                            unionid: $api.getStorage("shop_unionid"),
                            type: 1,
                            goods_id: that.goodsId
                        }
                    }

                    app.ajax(
                        url, {
                            data: {
                                values: arg
                            }
                        },
                        function(res) {
                            if (res.code == 0) {
                                if (!isCollect) {
                                    if (res.data) {
                                        that.collectId = res.data;
                                    }
                                }
                                that.isCollect = !that.isCollect;
                            } else {
                                api.toast({
                                    msg: res.message,
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            }
                        }
                    );
                }

            },

            // 选择商品型号\规格
            selectOption: function(name, id, price, stock, optionId, index) {
                var def_obj = {
                    price: price,
                    stock: stock,
                    name: name,
                    goods_id: id,
                    option_id: optionId
                };
                this.isSelectO = true;

                this.number = 1;
                for (var i = 0, len = $('.list-item-js').length; i < len; i++) {
                    $('.list-item-js').eq(i).css({
                        'color': '#000'
                    });
                }
                $('.list-item-js').eq(index).css({
                    'color': '#9F71FD'
                });

                this.choicInfo = def_obj;
                this.chocOption = this.goodsOptions[index];
            },

            // 购买数量
            goodsNum: function(type) {
                if (type == 'add') {
                    // 添加
                    if(this.goodsInfo.is_member_product!=1){
                      if (this.number < this.chocOption.stock) {
                          var num = parseInt(this.number);
                          num += 1;
                          this.number = num;
                      }
                    }
                } else if (type == 'reduce') {
                    // 减少
                    if (this.number >= 2) {
                        var num = parseInt(this.number);
                        num -= 1;
                        this.number = num;
                    }
                }
            },

            // 添加到购物车
            addToCart: function() {
                var that = this;
                var goodsId, optionId;
                var unionid = $api.getStorage("shop_unionid");
                var quantity = $('.number-goods').val();

                if (this.bs == "") { //游客模式，没有缓存
                    dialog.alert({
                        title: '游客模式',
                        msg: '体验更多功能，请先登录',
                        buttons: ['取消', '确定']
                    }, function(ret) {
                        if (ret.buttonIndex == 2) { //确认
                            api.closeToWin({
                                name: 'WeChat_login'
                            });
                        }
                    })
                } else {
                    if (that.choicInfo) {
                        goodsId = that.choicInfo.goods_id;
                        optionId = that.choicInfo.option_id;
                    } else {
                        this.goodsType(); // 若没有选择规格则弹出选择规格参数
                        api.toast({
                            msg: '请选择商品型号',
                            duration: 2000,
                            location: 'middle'
                        });
                        return false;
                    }

                    app.ajax(
                        service['addToCart'], {
                            data: {
                                values: {
                                    unionid: unionid,
                                    quantity: quantity,
                                    goods_id: goodsId,
                                    option_id: optionId
                                }
                            }
                        },
                        function(res) {
                            if (res.code == 0) {
                                api.toast({
                                    msg: '添加到购物车成功',
                                    duration: 2000,
                                    location: 'middle'
                                });
                                that.getCartCount();
                                // api.sendEvent({
                                //     name: 'lisentCart'
                                // });
                            } else {
                                api.toast({
                                    msg: res.message,
                                    duration: 2000,
                                    location: 'middle'
                                });
                            }
                        }
                    );
                }
            },

            // 立即购买
            buyNow: function() {
                var that = this;

                if (this.bs == "") { //游客模式，没有缓存
                    dialog.alert({
                        title: '游客模式',
                        msg: '体验更多功能，请先登录',
                        buttons: ['取消', '确定']
                    }, function(ret) {
                        if (ret.buttonIndex == 2) { //确认
                            api.closeToWin({
                                name: 'WeChat_login'
                            });
                        }
                    })
                } else {
                    if (!that.choicInfo) {
                        this.goodsType(); // 选择型号规格
                        api.toast({
                            msg: '请选择商品型号',
                            duration: 2000,
                            location: 'bottom'
                        });
                        return;
                    }
                    var quantity = $('.number-goods').val(); // 数量
                    var optionId = that.choicInfo.option_id; // 规格
                    var str = optionId + ',' + quantity + ',0,0';
                    var count = this.choicInfo.stock;
                    if (quantity > count) {
                        api.toast({
                            msg: '库存不足',
                            duration: 2000,
                            location: 'bottom'
                        });
                        return;
                    }

                    app.openWin('buyOrder', {
                        options: str
                    }); // 生成订单
                    this.$refs.goodsTypeDetail.classList.remove("active");
                    that.choicInfo = null;
                    for (var i = 0, len = $('.list-item-js').length; i < len; i++) {
                        $('.list-item-js').eq(i).css({
                            'color': '#000'
                        });
                    }
                }
            },

            // 触底
            scrolltobottom: function() {
                if (this.tabIndex == 1) {
                    if (this.hasMoreComment) {
                        this.commentPage += 1;
                        this.getComment();
                    }
                }
            },

            // 联系客服
            toChar: function() {
                var that = this;
                var obj = {
                    openid: that.otherUserInfo.openid, //id
                    nickname: that.shopInfo.shop_name, //昵称
                    src: that.shopInfo.shop_logo, //头像
                    new_nickname: that.otherUserInfo.new_nickname, //对象
                }
                console.log('abc...');
                console.log(JSON.stringify(obj));
                if (this.bs == "") { //游客模式，没有缓存
                    dialog.alert({
                        title: '游客模式',
                        msg: '体验更多功能，请先登录',
                        buttons: ['取消', '确定']
                    }, function(ret) {
                        if (ret.buttonIndex == 2) { //确认
                            api.closeToWin({
                                name: 'WeChat_login'
                            });
                        }
                    })
                } else {
                    app.yz_chat(obj);
                }
            },

            // 前往店铺首页
            toShop: function() {
              if (this.bs == "") { //游客模式，没有缓存
                  dialog.alert({
                      title: '游客模式',
                      msg: '体验更多功能，请先登录',
                      buttons: ['取消', '确定']
                  }, function(ret) {
                      if (ret.buttonIndex == 2) { //确认
                          api.closeToWin({
                              name: 'WeChat_login'
                          });
                      }
                  })
              }else{
                api.openWin({
                    name: 'single_shop',
                    url: '../shop_index/single_shop.html',
                    reload:true,
                    pageParam:{
                      out_user_id:this.shopInfo.out_user_id
                    }
                });
              }
            },

            // 前往其他商品
            toOtherGoods: function(id) {
                api.openFrame({
                    name: 'otherGoods',
                    url: './goods.html',
                    rect: {
                        x: 0,
                        y: 0,
                        w: 'auto',
                        h: 'auto'
                    },
                    pageParam: {
                        goodsId: id
                    },
                    reload: true
                });
            },

            // 提醒我
            clockMe: function(e) {
                var type = e.target.innerText;
                var that = this;

                var clockType;
                if (type.indexOf('提醒我') != -1) {
                    clockType = 1
                    if (type.indexOf('明天') != -1) {
                        clockType = 2;
                    }
                } else if (type.indexOf('已开启') != -1) {
                    return;
                }

                app.ajax(
                    service['indexGoodsRemind'], {
                        data: {
                            values: {
                                unionid: $api.getStorage("shop_unionid"),
                                goods_id: that.chocOption.goods_id,
                                remind_type: clockType
                            }
                        }
                    },
                    function(res) {
                        if (res.code == 0) {
                            api.toast({
                                msg: '已开启秒杀提醒',
                                duration: 2000,
                                location: 'bottom'
                            });
                            that.goodsInfo.is_remind = 1;
                        } else {
                            api.toast({
                                msg: res.message,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    }
                );
            },

            //--服务
            goodsServer: function(et) {
                this.$refs.goodsServeDetail.classList.add("active");
            },
            //--服务详细
            goodsServeDetailBg: function(et) {
                this.$refs.goodsServeDetail.classList.remove("active");
            },
            //--选择优惠券
            goodsCoupon: function(et) {
                this.$refs.goodsCouponDetail.classList.add("active");
            },
            //--选择优惠券详细
            goodsCouponDetailBg: function(et) {
                this.$refs.goodsCouponDetail.classList.remove("active");
            },
            //--选择类型
            goodsType: function(et) {
                this.$refs.goodsTypeDetail.classList.add("active");
            },
            //--选择类型详细
            goodsTypeDetailBg: function(et) {
                this.$refs.goodsTypeDetail.classList.remove("active");
                this.choicInfo = null;
                for (var i = 0, len = $('.list-item-js').length; i < len; i++) {
                    $('.list-item-js').eq(i).css({
                        'color': '#000'
                    });
                }
            },
            //--评价与详情
            goodsDetailTab: function(et) {
                this.commentPage = 1;
                var list = this.$refs.goodsDetailList.children;
                var index = +et.target.dataset.index;
                for (var i = 0; i < list.length; i++) {
                    list[i].classList.remove("active");
                }
                this.tabIndex = index;
                list[index].classList.add("active");

                if (et.target.dataset.index == 1) {
                    // 获取商品评论
                    if (!this.isClickComment) {
                        this.isClickComment = true;
                        this.getComment();
                    }

                }
            },
            showGoodsyon: function() {
                this.$refs.goodsyon.classList.add("active");
            },
            goodsyonBg: function() {
                this.$refs.goodsyon.classList.remove("active");
            },
            //--评论查看大图
            lookBigImg: function(et) {
                var ul = et.currentTarget.parentNode.querySelectorAll("img");
                var index = +et.currentTarget.dataset.index;
                var imgSrc = [];
                for (var i = 0; i < ul.length; i++) {
                    imgSrc.push(ul[i].src.replace(/\?x-oss-process=image\/resize,m_lfit,w_960/g, ''));
                }
                api.openWin({
                    name: 'lookBigImg',
                    url: 'widget://src/Shop/buy_person/big_img.html',
                    reload: true,
                    animation: {
                        type: "movein",
                        subType: "from_right",
                        duration: 200
                    },
                    pageParam: {
                        src: imgSrc,
                        idnex: index
                    }
                });
            },
            showBigImgs: function(url) {
                var photoBrowser = api.require('photoBrowser');
                var urlArr = [url];
                photoBrowser.open({
                    images: urlArr,
                    bgColor: '#000',
                    activeIndex: 0
                }, function(ret, err) {
                    if (ret.eventType == 'click') {
                        photoBrowser.close();
                    }
                });
            }
        },
        computed: {
            // 获取所在地省级地址
            locationChange: function() {
                return this.shopInfo.shop_location.split(' ')[1];
            },


            timeChange: function() {
                var that = this;
                var start = this.goodsInfo.date_start.split(' ');
                var now = that.startTime.split('-').join('/');

                var td = new Date();
                td = new Date(td.getFullYear(), td.getMonth(), td.getDate());
                var od = new Date(now);
                od = new Date(od.getFullYear(), od.getMonth(), od.getDate());
                var xc = (od - td) / 1000 / 60 / 60 / 24;
                if (xc < 0) {
                    return "已结束";
                } else if (xc == 0) {
                    return start[1] + '提醒我';
                } else if (xc < 2) {
                    return "明天" + start[1] + '提醒我';
                } else {
                    return "未开始";
                }
            },

            // 获取最高出售价
            countTopPrice: function() {
                var option = this.goodsOptions;
                if (option.length < 2) {
                    return this.chocOption.sale_price;
                } else {
                    var top = option[0].sale_price;
                    for (var i = 0, len = option.length; i < len; i++) {
                        if (top < option[i].sale_price) {
                            top = option[i].sale_price;
                        }
                    }
                    return top;
                }
            },
            // 获取最低出售价
            getLowPrice: function() {
                var option = this.goodsOptions;
                if (option.length < 2) {
                    return this.chocOption.sale_price;
                } else {
                    var price = option[0].sale_price;
                    for (var i = 0, len = option.length; i < len; i++) {
                        if (price > option[i].sale_price) {
                            price = option[i].sale_price;
                        }
                    }
                    return price;
                }
            },

            // 秒杀价
            getLowKillPrice: function() {
                var option = this.goodsOptions;
                if (option.length < 2) {
                    return this.chocOption.kill_price;
                } else {
                    var price = option[0].kill_price;
                    for (var i = 0, len = option.length; i < len; i++) {
                        if (price > option[i].kill_price) {
                            price = option[i].kill_price;
                        }
                    }
                    return price;
                }
            },

            // 获取最高市场价格
            getTopMarketPrice: function() {
                var option = this.goodsOptions;
                if (option.length < 2) {
                    return this.chocOption.market_price;
                } else {
                    var price = option[0].market_price;
                    for (var i = 0, len = option.length; i < len; i++) {
                        if (price < option[i].market_price) {
                            price = option[i].market_price;
                        }
                    }
                    return price;
                }
            },

            // 获取最低市场价
            getLowMarketPrice: function() {
                var option = this.goodsOptions;
                if (option.length < 2) {
                    return this.chocOption.market_price;
                } else {
                    var price = option[0].market_price;
                    for (var i = 0, len = option.length; i < len; i++) {
                        if (price > option[i].market_price) {
                            price = option[i].market_price;
                        }
                    }
                    return price;
                }
            }
        },
        mounted: function() {
            var self = this;
            // vue执行完毕后
            //--z支付返回刷新
            api.addEventListener({
                name: 'payBackGoods_'
            }, function(ret, val) {
                self.init();
            });

            //--点击上面按钮返回
            $api.addEvt($api.dom('#btn-back'), 'click', function() {
                api.closeWin()
            });

            //--右滑
            api.addEventListener({
                name: 'swiperight'
            }, function(ret, err) {
                api.closeWin();
            });

            $('.buy-head-cart').on('click', function() {
                if (self.bs == "") { //游客模式，没有缓存
                    dialog.alert({
                        title: '游客模式',
                        msg: '体验更多功能，请先登录',
                        buttons: ['取消', '确定']
                    }, function(ret) {
                        if (ret.buttonIndex == 2) { //确认
                            api.closeToWin({
                                name: 'WeChat_login'
                            });
                        }
                    })
                } else {
                    api.sendEvent({
                        name: 'toCart'
                    });
                }
            });
        }
    };
    var even = function() {
      api.addEventListener({
          name: 'goods_detail_back'
      }, function(ret, err){
        setTimeout(function(){
          api.closeWin()
        },230)
      });

        $('.buy-head-nav').on('click', function() {
            $('.nav-dio-top').toggle();
        });

        $('.nav-dio-top div').on('click', function() {
            var type = $(this).index();

            api.sendEvent({
                name: 'shopDetailDoShomeThing',
                extra: {
                    type: type,
                }
            });
        });
    }
    var newVue = new Vue(vm);
    even();
})
}
